import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

//封装博客表相关的操作
public class BlogDao {
    Connection connection=null;
    PreparedStatement preparedStatement=null;

    //插入博客---发布博客
    public void insert(Blog blog) throws SQLException {
        try{
            //和数据库建立连接
            connection=DBUtil.getConnection();
            //构造查询语句
            // Linux环境下 MySQL/mariaDB会大小写敏感
            //String sql="insert into Blog values(null,?,?,now(),?)";
            String sql="insert into blog values(null,?,?,now(),?)";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,blog.getTitle());
            preparedStatement.setString(2,blog.getContent());
            preparedStatement.setInt(3,blog.getUserId());
            //执行sql语句
            int ret=preparedStatement.executeUpdate();
            if(ret!=1){
                System.out.println("博客插入失败");
            }else{
                System.out.println("博客插入成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //这一步必须要执行到
            DBUtil.close(connection,preparedStatement,null);
        }


    }

    //播客详情页使用
    public Blog selectOne(int blogId) throws SQLException {
        ResultSet resultSet=null;
        try {
            connection=DBUtil.getConnection();

            String sql="select * from blog where blogId=?";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1,blogId);
            //发送
            resultSet=preparedStatement.executeQuery();
            //遍历结果集合 （一条数据，怎么得到各个属性）
            if(resultSet.next()){
                Blog blog=new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                return blog;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,preparedStatement,resultSet);
        }
        return null;
    }

    //博客列表页使用
    public List<Blog> selectAll() throws SQLException {
        List<Blog> list=new LinkedList<>();
        ResultSet resultSet=null;
        try {
            connection=DBUtil.getConnection();

            String sql="select * from blog order by postTime desc";
            preparedStatement=connection.prepareStatement(sql);
            //发送
            resultSet=preparedStatement.executeQuery();
            //遍历结果集合 （一条数据，怎么得到各个属性）
            while(resultSet.next()){
                Blog blog=new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                String content=resultSet.getString("content");
                if(content.length()>100){
                    content=content.substring(0,100)+"...";
                }
                blog.setContent(content);
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                list.add(blog);
            }


        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,preparedStatement,resultSet);
        }
        return list;
    }


    //删除博客
    public void delete(int blogId) throws SQLException {
        try{
            //和数据库建立连接
            connection=DBUtil.getConnection();
            //构造查询语句
            // Linux环境下 MySQL/mariaDB会大小写敏感
            //String sql="delete from Blog where blogId=?";
            String sql="delete from blog where blogId=?";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1,blogId);

            //执行sql语句
            int ret=preparedStatement.executeUpdate();
            if(ret!=1){
                System.out.println("博客删除失败");
            }else{
                System.out.println("博客删除成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //这一步必须要执行到
            DBUtil.close(connection,preparedStatement,null);
        }


    }
}
